package com.jx.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jx.service.CatService;
import com.jx.servicrimpl.CatServiceImpl;

public class DelCatController extends HttpServlet{
	//定义CatService引用指向CatServiceImpl对象，以调用service层方法
		CatService delcatservice = new CatServiceImpl();
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			// TODO Auto-generated method stub
			doPost(req, resp);
		}
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
			//设置字符集
			req.setCharacterEncoding("UTF8");
			resp.setCharacterEncoding("UTF8");
			//创建流对象
			PrintWriter pw = resp.getWriter();
			//定义字符串变量cids接收前台传来的参数
			String cids = req.getParameter("cids");
			//定义字符串数组pid，并将cids以逗号分隔
			String[] cid = cids.split(",");
			int j = 0;
			for (int i = 0; i < cid.length; i++) {
				int catID = Integer.parseInt(cid[i]);
				if (delcatservice.delCat(catID)) {
						j++;
				}else{
					break;
				}
			}
			if (j==cid.length) {
				pw.write("1");
			}
				//关闭流
				pw.flush();
				pw.close();
		}
}
